/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MShareInfoList.h"

#include <algorithm>

//---------------------------------------------------------------------------

MShareInfoList::MShareInfoList(void)
{
}

MShareInfoList::~MShareInfoList(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void MShareInfoList::CleanUp(void)
{
	MShareInfo *msi;

	for(iter=theVector.begin(); iter!=theVector.end(); iter++){
		msi = *iter;
		if(msi!=NULL){
			*iter = NULL;
			delete msi;
		}
	}
	theVector.clear();
}

//---------------------------------------------------------------------------

MShareInfo* MShareInfoList::CreateShareInfo(void)
{
	MShareInfo* msi;

	try{
		msi = new MShareInfo;
	}
	catch(...){
		msi = NULL;
	}
	
	return msi;
}

//---------------------------------------------------------------------------

MShareInfo* MShareInfoList::FindShareInfo(MShareInfo* msiIn)
{
	MShareInfo* msi;
	MShareInfo* res;

	if(msiIn==NULL) return NULL;

	res = NULL;
	for(iter=theVector.begin(); iter!=theVector.end() && res==NULL; iter++){
		msi = *iter;
		if(msi!=NULL){
			if(msi->type==msiIn->type)
				if(msi->name.Compare(msiIn->name)==0)
					res = msi;
		}
	}

	return res;
}

//---------------------------------------------------------------------------

bool MShareInfoList::Add(MShareInfo* msiIn)
{
	MShareInfo* msi;
	int cnt;

	if(msiIn==NULL) return true;	// nothing to add

	msi = CreateShareInfo();
	if(msi==NULL) return false;

	msi->LoadFromPointer(msiIn);

	cnt = (int)theVector.size();
	theVector.insert(theVector.begin(), msi);
	cnt = (int)theVector.size() - cnt;
	if(cnt!=1){
		delete msi;
		return false;
	}

	return true;
}

bool MShareInfoList::AddNoDuplicates(MShareInfo* msiIn)
{
	MShareInfo* msi;

	msi = FindShareInfo(msiIn);
	if(msi!=NULL) return true;

	return Add(msiIn);
}

void MShareInfoList::Clear(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

int MShareInfoList::GetSize(void)
{
	return (int)theVector.size();
}

MShareInfo* MShareInfoList::GetByIdx(int idxIn)
{
	if(idxIn<0)          return NULL;
	if(idxIn>=GetSize()) return NULL;

	return theVector[idxIn];
}

//---------------------------------------------------------------------------

bool MShareInfoGreater(MShareInfo* a1, MShareInfo* a2)
{
	if(a2==NULL) return true;
	if(a1==NULL) return false;
	
	if(a1->type!=a2->type)
		return a1->type < a2->type;

	return a1->name.CompareNoCase(a2->name)<0;
}

void MShareInfoList::SortTheList(void)
{
	std::sort(theVector.begin(), theVector.end(), MShareInfoGreater);
}

void MShareInfoList::RandomizeList(void)
{
	std::random_shuffle(theVector.begin(), theVector.end());
}

//---------------------------------------------------------------------------
